/**
 * Notes: 会场后台管理
 * Ver : CCMiniCloud Framework 2.0.1 ALL RIGHTS RESERVED BY cclinux0730 (wechat)
 * Date: 2022-06-23 07:48:00 
 */

const BaseProjectAdminService = require('./base_project_admin_service.js');
const util = require('../../../../framework/utils/util.js');
const cloudUtil = require('../../../../framework/cloud/cloud_util.js');
const cloudBase = require('../../../../framework/cloud/cloud_base.js');
const timeUtil = require('../../../../framework/utils/time_util.js');
const dataUtil = require('../../../../framework/utils/data_util.js');
const RoomModel = require('../../model/room_model.js');
const exportUtil = require('../../../../framework/utils/export_util.js');

class AdminRoomService extends BaseProjectAdminService {
	/**取得分页列表 */
	async getRoomList({
		search, // 搜索条件
		sortType, // 搜索菜单
		sortVal, // 搜索菜单
		orderBy, // 排序
		whereEx, //附加查询条件
		page,
		size,
		isTotal = true,
		oldTotal
	}) {
		orderBy = orderBy || {
			'ROOM_SEAT_CNT': 'asc',
			'ROOM_ADD_TIME': 'desc'
		};
		let fields = 'ROOM_TITLE,ROOM_STATUS,ROOM_CATE_ID,ROOM_CATE_NAME,ROOM_SEAT_CNT,ROOM_SEAT_LIST,ROOM_ORDER,ROOM_FORMS,ROOM_OBJ,ROOM_ADDRESS,ROOM_ADDRESS_GEO,ROOM_QR,ROOM_ADD_TIME,ROOM_EDIT_TIME';

		let where = {};
		where.and = {
			_pid: this.getProjectId() //复杂的查询在此处标注PID
		};

		if (util.isDefined(search) && search) {
			where.or = [{
				ROOM_TITLE: ['like', search]
			},];
		} else if (sortType && util.isDefined(sortVal)) {
			// 搜索菜单
			switch (sortType) {
				case 'cateId': {
					where.and.ROOM_CATE_ID = String(sortVal);
					break;
				}
				case 'status': {
					where.and.ROOM_STATUS = Number(sortVal);
					break;
				}
				case 'top': {
					where.and.ROOM_ORDER = 0;
					break;
				}
				case 'sort': {
					orderBy = this.fmtOrderBySort(sortVal, 'ROOM_ADD_TIME');
					break;
				}
			}
		}

		return await RoomModel.getList(where, fields, orderBy, page, size, isTotal, oldTotal);
	}

	/**获取信息 */
	async getRoomDetail(id) {
		let fields = '*';
		let where = {
			_id: id
		}

		let room = await RoomModel.getOne(where, fields);
		if (!room) return null;

		return room;
	}

	/**添加 */
	async insertRoom({
		title,
		order,
		seatCnt,
		seatList,
		address,
		addressGeo,
		forms
	}) {
		let data = {};
		data.ROOM_TITLE = title;
		data.ROOM_ORDER = order;
		data.ROOM_SEAT_CNT = seatCnt;
		data.ROOM_SEAT_LIST = seatList;
		data.ROOM_ADDRESS = address;
		data.ROOM_ADDRESS_GEO = addressGeo;
		data.ROOM_FORMS = forms
		return await RoomModel.insert(data);
	}

	//#############################   
	/** 清空 */
	async clearRoomAll(activityId) {
		this.AppError('该功能暂不开放');
	}

	/**删除数据 */
	async delRoom(id) {
		let where = {
			_id: id
		}
		await RoomModel.del(where);
	}

	// 更新forms信息
	async updateRoomForms({
		id,
		hasImageForms
	}) {
		return await RoomModel.editForms(id, 'ROOM_FORMS', 'ROOM_OBJ', hasImageForms);
	}

	/**更新数据 */
	async editRoom({
		id,
		title,
		seatCnt,
		seatList,
		address,
		addressGeo,
		order,
		forms,
		// joinForms
	}) {
		let where = {};
		where.and = {
			_pid: this.getProjectId(), //复杂的查询在此处标注PID
			_id: id
		};
		let data = {};
		data.ROOM_TITLE = title;
		data.ROOM_ORDER = order;
		data.ROOM_SEAT_CNT = seatCnt;
		data.ROOM_SEAT_LIST = seatList;
		data.ROOM_FORMS = forms;
		data.ROOM_ADDRESS = address;
		data.ROOM_ADDRESS_GEO = addressGeo;
		data.ROOM_EDIT_TIME = this._timestamp;
		await RoomModel.edit(where,data);
	}
	/**修改状态 */
	async statusRoom(id, status) {
		let where = {};
		where.and = {
			_pid: this.getProjectId(), //复杂的查询在此处标注PID
			_id: id
		};
		let data = {};
		data.ROOM_STATUS = status;
		await RoomModel.edit(where,data);
	}
}
module.exports = AdminRoomService;